<template>
  <div class="main">
    <div class="user-card">
      <!-- 头像添加边框容器 -->
      <div class="avatar-wrapper">
        <el-avatar :size="80" :src="userInfo.avatar"/>
      </div>

      <span class="nickname">{{ userInfo.nickname }}</span>
      <span class="follow statistics"><span class="num">{{ userInfo.followee }}</span>关注数</span>
      <span class="fans statistics"><span class="num">{{ userInfo.fans }}</span>粉丝数</span>
      <span class="likes statistics"><span class="num">{{ userInfo.likes }}</span>喜欢数</span>
      <span class="collect statistics"><span class="num">{{ userInfo.collects }}</span>收藏数</span>
      <span class="user-id">ID: {{ userInfo.id }} <span
        class="spit">|</span> {{ userInfo.introduced }}</span>
    </div>
    <span class = "analytics-p">数据总览</span>
    <div class="analytics">

      <el-tabs v-model="select" class="demo-tabs" type="card" >
        <el-tab-pane label="近7日" name="week">
          <span class="scan card">
            <span class="name">观看</span>
            <span class="num">{{ data.weekPersonalData.views }}</span>
          </span>
          <span class="like card">
            <span class="name">点赞</span>
            <span class="num">{{ data.weekPersonalData.likes }}</span>
             <span class="compare">{{calculateTrend(data.weekPersonalData.likes,data.weekPersonalData.likesCompare)}}</span>
          </span>
          <span class="collect card">
            <span class="name">收藏</span>
            <span class="num">{{ data.weekPersonalData.collects }}</span>
            <span class="compare">{{calculateTrend(data.weekPersonalData.collects,data.weekPersonalData.collectsCompare)}}</span>
          </span>
          <span class="comment card">
            <span class="name">评论</span>
            <span class="num">{{ data.weekPersonalData.comments }}</span>
            <span class="compare">{{calculateTrend(data.weekPersonalData.comments,data.weekPersonalData.commentsCompare)}}</span>
          </span>
          <span class="fans card">
            <span class="name">涨粉</span>
            <span class="num">{{ data.weekPersonalData.follows }}</span>
          </span>
        </el-tab-pane>
        <el-tab-pane label="近30天" name="month">
          <span class="scan card">
            <span class="name">观看</span>
            <span class="num">{{ data.monthPersonalData.views }}</span>
          </span>
          <span class="like card">
            <span class="name">点赞</span>
            <span class="num">{{ data.monthPersonalData.likes }}</span>
             <span class="compare">{{calculateTrend(data.monthPersonalData.likes,data.monthPersonalData.likesCompare)}}</span>
          </span>
          <span class="collect card">
            <span class="name">收藏</span>
            <span class="num">{{ data.monthPersonalData.collects }}</span>
            <span class="compare">{{calculateTrend(data.monthPersonalData.collects,data.monthPersonalData.collectsCompare)}}</span>
          </span>
          <span class="comment card">
            <span class="name">评论</span>
            <span class="num">{{ data.monthPersonalData.comments }}</span>
            <span class="compare">{{calculateTrend(data.monthPersonalData.comments,data.monthPersonalData.commentsCompare)}}</span>
          </span>
          <span class="fans card">
            <span class="name">涨粉</span>
            <span class="num">{{ data.monthPersonalData.follows}}</span>
          </span>
        </el-tab-pane>
      </el-tabs>
    </div>

    <div class="posts">
      <span>近期创作</span>
      <div class="post-list">
        <div class="post-item" v-for="post in posts" :key="post.id">
          <h3 class="post-title">{{ post.title }}</h3>
          <p v-html="post.content"></p>
          <span>浏览数: {{post.views}}</span>
          <span>喜欢数：{{ post.likeCount }}</span>
          <span>收藏数：{{ post.collectCount }}</span>
          <span>评论数：{{ post.commentCount }}</span>
          <span>{{post.createTime}}</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import {ref} from "vue";
import {getMyPostData, getMyUserInfo, getPersonalData} from "@/api/publish.js";

const userInfo = ref({
})

const getUserInfo = async () =>{
  const res = await getMyUserInfo()
  userInfo.value = res.data.data
}
getUserInfo()

const select = ref('week')


const data = ref({
  monthPersonalData: {},
  weekPersonalData: {}
});
const getData = async () =>{
  const res = await getPersonalData()
  data.value = res.data.data
  console.log(data.value.monthPersonalData)
}
getData()

const posts = ref([
])

const getPosts = async () =>{
  const res = await getMyPostData()
  posts.value = res.data.data
}
getPosts()
// 工具函数：计算变化率
const calculateTrend = (current, compare) => {
  // 处理非数字或负数对比值
  if (typeof current !== 'number' || typeof compare !== 'number' || compare <= 0 || current <= 0) {
    return ;
  }

  // 正常计算变化率
  const absolute = current - compare;
  if(absolute===0) return ;
  const percent = (absolute / compare) * 100;

  // 格式化百分比（带正负号，保留一位小数）
  const formattedPercent = percent.toFixed(1);
  const relative = `${percent >= 0 ? '+' : ''}${formattedPercent}%`;

  return relative;
};
</script>

<style scoped lang="scss">
.user-card {
  background: linear-gradient(135deg, #88b5d2, #ffffff);
  backdrop-filter: blur(10px);
  border-radius: 1rem;
  padding: 1.5rem;
  margin-bottom: 1.5rem;
  position: relative;
  border: 1px solid rgba(255, 255, 255, 0.3);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.08);
  transition: all 0.3s ease;

  &:hover {
    box-shadow: 0 12px 32px rgba(0, 0, 0, 0.12);
    transform: translateY(-3px);
  }

  // 头像边框样式
  .avatar-wrapper {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 88px;  /* 比头像大4px */
    height: 88px;
    border-radius: 50%;
    background: linear-gradient(135deg, #55a8da, #ffffff);  /* 渐变边框 */
    padding: 5px;  /* 边框宽度 */
    box-shadow: 0 4px 12px rgba(64, 158, 255, 0.2);  /* 头像阴影 */
    transition: transform 0.3s ease, box-shadow 0.3s ease;

    &:hover {
      transform: scale(1.05);  /* 悬停放大效果 */
      box-shadow: 0 6px 16px rgba(64, 158, 255, 0.3);  /* 悬停加深阴影 */
    }

    :deep(.el-avatar) {
      width: 100% !important;
      height: 100% !important;
      border-radius: 50%;
      background-color: #f0f2f5;  /* 缺省头像背景色 */
    }
  }

  .nickname {
    position: absolute;
    left: 8.125rem;
    font-size: 1.375rem;
    font-family: 'serif', '微软雅黑', sans-serif;
    margin-bottom: 0.625rem;
    font-weight: 600;
    color: #2c3e50;
    text-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
  }

  .statistics {
    position: absolute;
    top: 3.4375rem;
    color: #ffffff;
    font-family: 'serif', '微软雅黑', sans-serif;
    font-size: 0.875rem;
    transition: color 0.3s ease;
  }

  .statistics:hover {
    color:#e89bb8;
  }

  .follow {
    left: 8.125rem;
  }

  .fans {
    left: 13.125rem;
  }

  .likes {
    left: 18.125rem;
  }

  .collect {
    left: 23.125rem;
  }

  .num {
    margin-right: 0.3125rem;
    font-size: 0.9375rem;
    color: #ffffff;
    font-weight: 500;
    position: relative;
    padding: 0 2px;

    &::after {
      content: '';
      position: absolute;
      bottom: -2px;
      left: 0;
      width: 0;
      height: 2px;
      background-color: #e89bb8;
      transition: width 0.3s ease;
    }
  }

  .statistics:hover .num::after {
    width: 100%;
  }

  .user-id {
    font-size: 0.875rem;
    color: #95a5a6;
    position: absolute;
    left: 8.125rem;
    top: 5.3125rem;
    display: flex;
    align-items: center;
    gap: 0.5rem;
    background-color: rgba(245, 247, 250, 0.8);
    padding: 0.25rem 0.75rem;
    border-radius: 12px;
  }

  .spit {
    color: #dcdde1;
    width: 1px;
    height: 0.875rem;
    background-color: #dcdde1;
    border-radius: 0;
  }
}

@media (max-width: 768px) {
  .user-card {
    padding: 2rem 1.5rem 1.5rem;
    display: flex;
    flex-direction: column;
    align-items: center;
    text-align: center;

    .avatar-wrapper {
      margin-bottom: 1rem;
    }
  }

  .nickname,
  .statistics,
  .user-id {
    position: static;
    margin: 0.5rem 0;
  }

  .statistics {
    display: inline-block;
    margin: 0 0.75rem;
  }

  .user-id {
    margin-top: 1rem;
    justify-content: center;
  }
}

.analytics {
  background-color: rgb(255, 255, 255);
  border-radius: 2rem;
  padding: 2rem;
  margin-bottom: 2rem;
  backdrop-filter: blur(8px);
  .card {
    display: inline-block;
    width: 7.8125rem;
    height: 5.3125rem;
    border: 3px solid rgb(150, 202, 234);
    margin-right: 0.75rem;
    border-radius: 0.375rem;
    position: relative;

    .name {
      position: absolute;
      left: 2.6rem;
      top: 0.625rem;
      color: #7a7a7a;
      font-size: 14px;
    }

    .num {
      position: absolute;
      left: 3.3rem;
      top: 2.1875rem;
      color: #faa0af;
    }

    .compare {
      position: absolute;
      left: 0.625rem;
      top: 3.75rem;
      color: #ed0b38;
      font-size: 13px;
    }
  }
}
.analytics-p{
  display: block;
  font-size: 1.5rem;
  font-weight: 600;
  color: #1a1a1a;
  margin-bottom: 1.5rem;
  padding-left: 0.5rem;
  border-left: 4px solid #3fc4f1;
}
@keyframes bounce {
  0% {
    transform: translateY(0);
    box-shadow: 0 4px 12px rgba(59, 130, 246, 0.1);
  }
  100% {
    transform: translateY(-8px);
    box-shadow: 0 12px 20px rgba(59, 130, 246, 0.15);
  }
}

.posts {
  max-width: 1200px;
  margin: 2rem auto;
  padding: 0 1rem;

  > span {
    display: block;
    font-size: 1.5rem;
    font-weight: 600;
    color: #1a1a1a;
    margin-bottom: 1.5rem;
    padding-left: 0.5rem;
    border-left: 4px solid #3fc4f1;

  }

  .post-list {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
    gap: 1.5rem;

    .post-item {
      background: #ffffff;
      border-radius: 8px;
      padding: 1.5rem;
      box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
      transition: transform 0.2s ease;
      &:hover {
        box-shadow: 0 12px 32px rgba(0, 0, 0, 0.12);
        transform: translateY(-3px);
      }
      .post-title {
        font-size: 1.2rem;
        font-weight: 500;
        color: #303133;
        margin: 0 0 0.8rem 0;
        line-height: 1.4;

        display: -webkit-box;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
        overflow: hidden;
      }

      p {
        color: #606266;
        font-size: 0.95rem;
        line-height: 1.6;
        margin: 0 0 1rem 0;

        display: -webkit-box;
        -webkit-line-clamp: 3;
        -webkit-box-orient: vertical;
        overflow: hidden;
      }

      p > * {
        display: inline;
        margin: 0;
      }

      > span {
        display: inline-block;
        font-size: 0.85rem;
        color: rgb(59, 120, 157);
        margin-right: 1.2rem;
        padding: 0.3rem 0.6rem;
        background: #ffffff;
        border-radius: 4px;

        &:last-child {
          margin-right: 0;
        }
      }
    }
  }
}
</style>
